/***
 * 控制器
 */
const _ = require('lodash')
const svgCaptcha = require('svg-captcha')
const MD5  = require('md5')
const JWT = require('../utils/jwt')
const { nanoid } = require('nanoid')
const dateFormat = require('../utils/date')
const globalSendResult = require('../utils/globalSendResult')
const UserServices = require('../services/UserServices')
const UsersController = {
    add: (req,res) => {
       const postData = _.clone(req.body)
       const userObj = { userid: nanoid(), add_time: dateFormat(), password: MD5(postData.password) }
        globalSendResult({
            promiseFunc: UserServices.add(Object.assign(postData,userObj)),
            response: res,
            errMsg: '新增用户失败',
            msg: '新增用户',
            isReturn: false
        })
    },
    edit: (req,res) => {
        const editData = _.clone(req.body)
        console.log(editData)
        editData.add_time = dateFormat()
        globalSendResult({
            promiseFunc: UserServices.edit(editData),
            response: res,
            errMsg: '修改用户失败',
            msg: '修改用户',
            isReturn: false
        })
    },
    deleteOne: (req,res) =>{
        const { userid } = req.body
        globalSendResult({
            promiseFunc: UserServices.deleteOne(userid),
            response: res,
            errMsg: '删除用户失败',
            msg: '删除用户',
            isReturn: false
        })
    },
    queryOne: (req,res) => {
        const { userid } = req.query
        globalSendResult({
            promiseFunc: UserServices.queryOne(userid),
            response: res,
            errMsg: '查询用户失败',
            msg: '查询用户',
        })
    },
    query: (req,res) => {
        const queryData = _.clone(req.query || {})
        globalSendResult({
            promiseFunc: UserServices.query(queryData),
            response: res,
            errMsg: '查询用户列表失败',
            msg: '查询用户列表',
            countTotal: true,
            queryData,
            Services: UserServices
        })
    },
    login: (req,res) => {
        const loginData = _.clone(req.body)
        // 判断验证码
        if(loginData.captcha !== req.session.captcha) {
            return res.send({
                code: 0,
                msg: '验证码错误'
            })
        }
        UserServices.login(loginData)
            .then(result => {
                if(result) {
                    const { username, password, userid, email, status} = result
                    if(!status)  {
                        // 已经被禁用的用户账号不允许登录
                        return Promise.reject('当前账号已被禁用，请联系管理员')
                    }
                    const jwtInfo =  {
                        username,
                        password,
                        userid,
                        email
                    }
                    const token = JWT.generate(jwtInfo)
                    res.send({
                        code: 1,
                        msg: '登录成功',
                        data: result,
                        Authorization: token
                    })
                } else  {
                    res.send({
                        code: 0,
                        msg: '账号或者密码错误',
                    })
                }
            }).catch(e => {
            res.send({
                code: 0,
                msg: e || '登录失败',
            })
        })
    },
    getCaptcha: async (req,res) => {
        const cap = svgCaptcha.create({
            size: 4,
            color: true,
            fontSize: 40,
            // 噪声线条数
            noise: 5,
            // 宽度
            width: 100,
            // 高度
            height: 35,
            background: '#eeeeee',
        })
        req.session.captcha = cap.text.toLocaleLowerCase(); // session 存储验证码数值
        res.type('image/svg+xml'); // 响应的类型
        res.send(cap.data)
    },
}

module.exports = UsersController
